﻿#nullable enable

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PHMEE.Controllers;

namespace PHMEE.EnergyEfficiency;

/// <summary>
/// 燃油管理
/// </summary>
[Route("api/cbm/fuel")]
public class FuelController : PHMEEController, IFuelAppService
{
    private readonly IFuelAppService _fuelAppService;

    public FuelController(IFuelAppService fuelAppService)
    {
        _fuelAppService = fuelAppService;
    }

    // /// <summary>
    // /// 当前主机、副机、锅炉所用燃料类型
    // /// </summary>
    // /// <param name="vesselId"></param>
    // /// <returns></returns>
    // [Obsolete]
    // [Route("CurrentFuelType")]
    // [HttpGet]
    // public Task<CurrentFuelTypeDto> CurrentFuelTypeAsync(Guid vesselId)
    // {
    //     return _fuelAppService.CurrentFuelTypeAsync(vesselId);
    // }

    // /// <summary>
    // /// 小时油耗量
    // /// </summary>
    // /// <param name="vesselId"></param>
    // /// <param name="hour"></param>
    // /// <returns></returns>
    // [Obsolete]
    // [HttpGet]
    // [Route("FuelConsumptionForHour")]
    // public Task<IReadOnlyList<FuelMeasurement>> FuelConsumptionForHour(Guid vesselId,
    //     DateTime? hour)
    // {
    //     return _fuelAppService.FuelConsumptionForHour(vesselId, hour);
    // }

    /// <summary>
    /// 日油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="date"></param>
    /// <returns></returns>
    [Route("FuelConsumptionForDay")]
    [HttpGet]
    public Task<FuelConsumptionDto> FuelConsumptionForDay(Guid vesselId, DateTime? date)
    {
        return _fuelAppService.FuelConsumptionForDay(vesselId, date);
    }

    /// <summary>
    /// 油耗量查询（具体到分钟）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTime"></param>
    /// <param name="endDateTime"></param>
    /// <returns></returns>
    [Route("FuelConsumptionForDateTimePeriod")]
    [HttpGet]
    public Task<FuelConsumptionDto> FuelConsumptionForDateTimePeriod(Guid vesselId, DateTime startDateTime,
        DateTime endDateTime)
    {
        return _fuelAppService.FuelConsumptionForDateTimePeriod(vesselId, startDateTime, endDateTime);
    }

    /// <summary>
    /// 航次油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="voyageStartTime"></param>
    /// <param name="voyageEndTime"></param>
    /// <returns></returns>
    [Route("FuelConsumptionForVoyage")]
    [HttpGet]
    public Task<FuelConsumptionDto> FuelConsumptionForVoyage(Guid vesselId, DateTime voyageStartTime,
        DateTime voyageEndTime)
    {
        return _fuelAppService.FuelConsumptionForVoyage(vesselId, voyageStartTime, voyageEndTime);
    }

    /// <summary>
    /// 年油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="year"></param>
    /// <returns></returns>
    [Route("FuelConsumptionForYear")]
    [HttpGet]
    public Task<FuelConsumptionDto> FuelConsumptionForYear(Guid vesselId, int? year)
    {
        return _fuelAppService.FuelConsumptionForYear(vesselId, year);
    }

    /// <summary>
    /// 应急油耗-日油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="date"></param>
    /// <returns></returns>
    [Route("EmrgFuelConsumptionForDay")]
    [HttpGet]
    public Task<FuelConsumptionDto> EmrgFuelConsumptionForDay(Guid vesselId, DateTime? date)
    {
        return _fuelAppService.EmrgFuelConsumptionForDay(vesselId, date);
    }

    /// <summary>
    /// 应急油耗-油耗量查询（具体到分钟）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTime"></param>
    /// <param name="endDateTime"></param>
    /// <returns></returns>
    [Route("EmrgFuelConsumptionForDateTimePeriod")]
    [HttpGet]
    public Task<FuelConsumptionDto> EmrgFuelConsumptionForDateTimePeriod(Guid vesselId, DateTime startDateTime,
        DateTime endDateTime)
    {
        return _fuelAppService.EmrgFuelConsumptionForDateTimePeriod(vesselId, startDateTime, endDateTime);
    }

    /// <summary>
    /// 应急油耗-航次油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="voyageStartTime"></param>
    /// <param name="voyageEndTime"></param>
    /// <returns></returns>
    [Route("EmrgFuelConsumptionForVoyage")]
    [HttpGet]
    public Task<FuelConsumptionDto> EmrgFuelConsumptionForVoyage(Guid vesselId, DateTime voyageStartTime,
        DateTime voyageEndTime)
    {
        return _fuelAppService.EmrgFuelConsumptionForVoyage(vesselId, voyageStartTime, voyageEndTime);
    }

    /// <summary>
    /// 应急油耗-年油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="year"></param>
    /// <returns></returns>
    [Route("EmrgFuelConsumptionForYear")]
    [HttpGet]
    public Task<FuelConsumptionDto> EmrgFuelConsumptionForYear(Guid vesselId, int? year)
    {
        return _fuelAppService.EmrgFuelConsumptionForYear(vesselId, year);
    }

    /// <summary>
    /// 混合油耗-日油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="date"></param>
    /// <returns></returns>
    [Route("MixFuelConsumptionForDay")]
    [HttpGet]
    public Task<FuelConsumptionDto> MixFuelConsumptionForDay(Guid vesselId, DateTime? date)
    {
        return _fuelAppService.MixFuelConsumptionForDay(vesselId, date);
    }

    /// <summary>
    /// 混合油耗-油耗量查询（具体到分钟）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTime"></param>
    /// <param name="endDateTime"></param>
    /// <returns></returns>
    [Route("MixFuelConsumptionForDateTimePeriod")]
    [HttpGet]
    public Task<FuelConsumptionDto> MixFuelConsumptionForDateTimePeriod(Guid vesselId, DateTime startDateTime, DateTime endDateTime)
    {
        return _fuelAppService.MixFuelConsumptionForDateTimePeriod(vesselId, startDateTime, endDateTime);
    }

    /// <summary>
    /// 混合油耗-航次油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="voyageStartTime"></param>
    /// <param name="voyageEndTime"></param>
    /// <returns></returns>
    [Route("MixFuelConsumptionForVoyage")]
    [HttpGet]
    public Task<FuelConsumptionDto> MixFuelConsumptionForVoyage(Guid vesselId, DateTime voyageStartTime, DateTime voyageEndTime)
    {
        return _fuelAppService.MixFuelConsumptionForVoyage(vesselId, voyageStartTime, voyageEndTime);
    }

    /// <summary>
    /// 混合油耗-年油耗量
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="year"></param>
    /// <returns></returns>
    [Route("MixFuelConsumptionForYear")]
    [HttpGet]
    public Task<FuelConsumptionDto> MixFuelConsumptionForYear(Guid vesselId, int? year)
    {
        return _fuelAppService.MixFuelConsumptionForYear(vesselId, year);
    }

    /// <summary>
    /// 油品即时存量显示
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    [Route("FuelInventory")]
    [HttpGet]
    public Task<IReadOnlyList<FuelMeasurement>> FuelInventoryAsync(Guid vesselId, DateTime? time)
    {
        return _fuelAppService.FuelInventoryAsync(vesselId, time);
    }

    /// <summary>
    /// 录入油品存量显示（数据来自 LogAbstract 的 ROB）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    [Route("FuelInventoryFromLogAbstract")]
    [HttpGet]
    public Task<IReadOnlyList<FuelMeasurement>> FuelInventoryFromLogAbstractAsync(Guid vesselId, DateTime? time)
    {
        return _fuelAppService.FuelInventoryFromLogAbstractAsync(vesselId, time);
    }

    /// <summary>
    /// 时间段平均油耗量查询对比（单位为小时）具体到分钟
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="ts1StartDateTime"></param>
    /// <param name="ts1EndDateTime"></param>
    /// <param name="ts2StartDateTime"></param>
    /// <param name="ts2EndDateTime"></param>
    /// <returns></returns>
    [Route("AverageFuelConsumptionComparison")]
    [HttpGet]
    public Task<IReadOnlyList<FuelConsumptionComparisonDto>> AverageFuelConsumptionComparison(Guid vesselId, DateTime ts1StartDateTime, DateTime ts1EndDateTime,
        DateTime ts2StartDateTime, DateTime ts2EndDateTime)
    {
        return _fuelAppService.AverageFuelConsumptionComparison(vesselId, ts1StartDateTime, ts1EndDateTime, ts2StartDateTime, ts2EndDateTime);
    }

    /// <summary>
    /// 航次平均油耗量对比（单位为小时）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="voyage1StartTime"></param>
    /// <param name="voyage1EndTime"></param>
    /// <param name="voyage2StartTime"></param>
    /// <param name="voyage2EndTime"></param>
    /// <returns></returns>
    [Route("VoyageAverageFuelConsumptionComparison")]
    [HttpGet]
    public Task<IReadOnlyList<FuelConsumptionComparisonDto>> VoyageAverageFuelConsumptionComparison(Guid vesselId, DateTime voyage1StartTime, DateTime voyage1EndTime,
        DateTime voyage2StartTime, DateTime voyage2EndTime)
    {
        return _fuelAppService.VoyageAverageFuelConsumptionComparison(vesselId, voyage1StartTime, voyage1EndTime, voyage2StartTime, voyage2EndTime);
    }

    /// <summary>
    /// 年度平均油耗量对比（单位为小时）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="year1"></param>
    /// <param name="year2"></param>
    /// <returns></returns>
    [Route("YearAverageFuelConsumptionComparison")]
    [HttpGet]
    public Task<IReadOnlyList<FuelConsumptionComparisonDto>> YearAverageFuelConsumptionComparison(Guid vesselId, int year1, int year2)
    {
        return _fuelAppService.YearAverageFuelConsumptionComparison(vesselId, year1, year2);
    }
    
    // /// <summary>
    // /// 发电柴油机即时油耗和油品
    // /// </summary>
    // /// <param name="vesselId"></param>
    // /// <returns></returns>
    // [Route("DieselGeneratorsInstantFuelConsumption")]
    // [HttpGet]
    // public Task<FuelConsumptionWithTypeDto> DieselGeneratorsInstantFuelConsumptionAsync(Guid vesselId)
    // {
    //     return _fuelAppService.DieselGeneratorsInstantFuelConsumptionAsync(vesselId);
    // }

    /// <summary>
    /// time 前的单次加油量查询
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="time"></param>
    /// <returns></returns>
    [Route("RefuelSingleTime")]
    [HttpGet]
    public Task<IEnumerable<FuelMeasurement>> RefuelSingleTimeAsync(Guid vesselId, DateTime? time)
    {
        return _fuelAppService.RefuelSingleTimeAsync(vesselId, time);
    }

    /// <summary>
    /// 加油量查询（日期段）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <returns></returns>
    [Route("RefuelSummaryInDateRange")]
    [HttpGet]
    public Task<IEnumerable<FuelMeasurement>> RefuelSummaryInDateRangeAsync(Guid vesselId, [Required] DateTime startDate,
        [Required] DateTime endDate)
    {
        return _fuelAppService.RefuelSummaryInDateRangeAsync(vesselId, startDate, endDate);
    }

    /// <summary>
    /// 加油量查询（年）
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="year"></param>
    /// <returns></returns>
    [Route("RefuelSummaryInYear")]
    [HttpGet]
    public Task<IEnumerable<FuelMeasurement>> RefuelSummaryInYearAsync(Guid vesselId, [Required] int? year)
    {
        return _fuelAppService.RefuelSummaryInYearAsync(vesselId, year);
    }

    // /// <summary>
    // /// 每个燃油流量计采集到的最新的值
    // /// </summary>
    // /// <param name="vesselId"></param>
    // /// <returns></returns>
    // [Route("FuelFlowMeterValues")]
    // [HttpGet]
    // public Task<FuelFlowMeterValues> FuelFlowMeterValuesAsync(Guid vesselId)
    // {
    //     return _fuelAppService.FuelFlowMeterValuesAsync(vesselId);
    // }

    /// <summary>
    /// 油耗分析接口
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="period"></param>
    /// <param name="fuelType"></param>
    /// <returns></returns>
    [Route("FuelConsumptionAnalyze")]
    [HttpGet]
    public Task<List<FuelConsumptionAnalyzeDto>> FuelConsumptionAnalyzeAsync(Guid vesselId, FuelConsumptionAnalyzePeriod period, FuelType fuelType)
    {
        return _fuelAppService.FuelConsumptionAnalyzeAsync(vesselId, period, fuelType);
    }

    /// <summary>
    /// 检查 LA Fuel 密码是否正确
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="laFuelPassword"></param>
    /// <returns></returns>
    [Route("CheckLAFuelPassword")]
    [HttpPost]
    public Task<bool> CheckLAFuelPasswordAsync(Guid vesselId, string laFuelPassword)
    {
        return _fuelAppService.CheckLAFuelPasswordAsync(vesselId, laFuelPassword);
    }

    /// <summary>
    /// 修改 LA Fuel 密码
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="newLaFuelPassword"></param>
    /// <returns></returns>
    [Route("ChangeLAFuelPassword")]
    [HttpPost]
    public Task<bool> ChangeLAFuelPasswordAsync(Guid vesselId, string newLaFuelPassword)
    {
        return _fuelAppService.ChangeLAFuelPasswordAsync(vesselId, newLaFuelPassword);
    }
}